package robotinterface;

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.*;
import robotinterface.client.RobotClient;
import robotinterface.client.RobotHandler;
import robotinterface.list.*;
import robotinterface.list.entry.*;
import robotinterface.list.listeners.*;


/**
 *
 * @author Nathan Hemmes / Brian Bailey
 */
public class SavePanel extends JPanel{

    /* List Stuff */
    RobotList robotList;
    RobotListModel robotListModel;
    ListTransferListener listTransferListener;

    public SavePanel(){
        //JPanel savePan = new JPanel();
        SpringLayout saveLay = new SpringLayout();
        setLayout(saveLay);
        setBorder(BorderFactory.createEtchedBorder());

        JLabel saveLabel = new JLabel("Positions List");

        /* Scroll Panel for JList */
        JScrollPane scrollPane = new JScrollPane();	
        scrollPane.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );

        ArrayList<Entry> listData = new ArrayList<>(
                Arrays.asList( new Entry("Home"), new Entry("Wave"),
                new Entry("Position 1"), new Entry("(1.11, 2.09, 0.22)"),
                new Entry("Position 3"), new Entry("Position 4"),
                new Entry("Shake"), new Entry("Go Down"),
                new Entry("High Five")) );

        /* RobotList and ListModel */
        robotListModel = new RobotListModel(listData);
        robotList = new RobotList(robotListModel);
        
        robotList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        robotList.setSelectionForeground(Color.LIGHT_GRAY);
        robotList.setSelectionBackground(Color.DARK_GRAY);
        robotList.setFixedCellHeight(20);
        robotList.setSelectedIndex(0);
        
        /* Sets RobotHandler for RobotList and RobotClient*/
        RobotHandler rHandler = new RobotHandler(robotList);
        RobotClient.setHandler(rHandler);

        /* Renderer for RobotList Cells */        
        RobotCellRenderer robotCellRenderer = new RobotCellRenderer();
        robotList.setCellRenderer( robotCellRenderer );

        /* EntryList Drag-N-Drop */
        robotList.setDragEnabled(true);
        robotList.setDropMode( DropMode.ON_OR_INSERT );
        robotList.setTransferHandler( new ListTransferHandler( robotList ) );
        robotList.setListDragListener( new ListTransferListener( robotList ) );

        //Buttons
        SaveButtonPanel saveButtonPanel = new SaveButtonPanel(robotList);

        /* Add Scrolling to Jlist */
        scrollPane.setViewportView( robotList );

        //Position
        add(saveLabel);
        saveLay.putConstraint(SpringLayout.WEST, saveLabel, 55, SpringLayout.WEST, this);
        saveLay.putConstraint(SpringLayout.NORTH, saveLabel, 5, SpringLayout.NORTH, this);
        saveLay.putConstraint(SpringLayout.EAST, saveLabel, -55, SpringLayout.EAST, this);
        saveLay.putConstraint(SpringLayout.SOUTH, saveLabel, 30, SpringLayout.NORTH, this);

        add(scrollPane);
        saveLay.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, this);
        saveLay.putConstraint(SpringLayout.NORTH, scrollPane, 10, SpringLayout.SOUTH, saveLabel);
        saveLay.putConstraint(SpringLayout.EAST, scrollPane, 0, SpringLayout.EAST, this);
        saveLay.putConstraint(SpringLayout.SOUTH, scrollPane, -50, SpringLayout.SOUTH, this);

        add(saveButtonPanel);
        saveLay.putConstraint(SpringLayout.WEST, saveButtonPanel, 0, SpringLayout.WEST, this);
        saveLay.putConstraint(SpringLayout.NORTH, saveButtonPanel, 10, SpringLayout.SOUTH, scrollPane);
        saveLay.putConstraint(SpringLayout.EAST, saveButtonPanel, 0, SpringLayout.EAST, this);
        saveLay.putConstraint(SpringLayout.SOUTH, saveButtonPanel, 0, SpringLayout.SOUTH, this);

    }
}
